package com.yijiu.trb;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.graphics.PixelFormat;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.yijiu.action.BaseAction;
import com.yijiu.network.Request;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.resource.CacheData;
import com.yijiu.response.RequestBrokerageDetailResponse;
import com.yijiu.utils.ConvertUtils;
import com.yijiu.utils.ImageUtils;

/**
 * 佣金详情
 * 
 * @author TangChao
 */
public class BrokerageDetailActivity extends BTitleActivity
{
	private String tradeId;

	private String detailId;

	private String investDetailId;

	/**
	 * 投资人姓名控件
	 */
	private TextView investorName;

	/**
	 * 投资人姓名
	 */
	private String investorNameString;

	/**
	 * 投资金额
	 */
	private TextView investMoney;

	/**
	 * 投资日期
	 */
	private TextView investData;

	/**
	 * 我的佣金
	 */
	private TextView inCome;

	/**
	 * 项目名称
	 */
	private TextView projectName;

	/*
	 * 需求金额
	 */
	private TextView needMoney;

	/**
	 * 年化收益率
	 */
	private TextView yield;

	/**
	 * 期限
	 */
	private TextView deadline;

	/**
	 * 兑付方式
	 */
	private TextView payType;

	/**
	 * 成立日期
	 */
	private TextView startTime;

	/**
	 * 到期日期
	 */
	private TextView endTime;

	/**
	 * 担保机构
	 */
	private TextView dbTeamTextView;

	/**
	 * 担保话语
	 */
	private TextView dbWordTextView;

	/**
	 * 担保图片标签
	 */
	private TextView dbTitle;

	/**
	 * 担保图片
	 */
	private ImageView imageView;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.RGBA_8888);
		setContentView(R.layout.brokerage_detail_layout);
		initWidget();
		// String appKey = new DataSP(this,
		// C.BASE_XML_NAME).getSValue(C.APP_KEY, null);
		String appKey = CacheData.APP_KEY;
		tradeId = getIntent().getIntExtra("tradeId", 0) + "";
		detailId = getIntent().getIntExtra("detailId", 0) + "";
		investDetailId = getIntent().getIntExtra("investDetailId", 0) + "";
		investorNameString = getIntent().getStringExtra("investorName");
		RequestBrokerageDetail(appKey, tradeId, detailId, investDetailId);
		Log.i("appKey", appKey);
		Log.i("tradeId", tradeId);
		Log.i("detailId", detailId);
		Log.i("investDetailId", investDetailId);
	}

	/**
	 * 初始化控件
	 */
	protected void initWidget()
	{
		titleView.setText("收益详情");
		investorName = (TextView) findViewById(R.id.tv_brokerage_details_realname);
		investMoney = (TextView) findViewById(R.id.tv_brokerage_details_money);
		investData = (TextView) findViewById(R.id.tv_brokerage_details_date);
		inCome = (TextView) findViewById(R.id.tv_brokerage_details_my_money);
		projectName = (TextView) findViewById(R.id.tv_brokerage_details_project_name);
		needMoney = (TextView) findViewById(R.id.tv_brokerage_details_need_money);
		yield = (TextView) findViewById(R.id.tv_brokerage_details_year_earnings);
		deadline = (TextView) findViewById(R.id.tv_brokerage_details_how_long);
		payType = (TextView) findViewById(R.id.tv_brokerage_details_way);
		startTime = (TextView) findViewById(R.id.tv_brokerage_details_found_date);
		endTime = (TextView) findViewById(R.id.tv_brokerage_details_arrive_date);
		dbTeamTextView = (TextView) findViewById(R.id.tv_brokerage_details_db_team);
		dbWordTextView = (TextView) findViewById(R.id.tv_brokerage_details_db_word);
		dbTitle = (TextView) findViewById(R.id.tv_brokerage_details_dbTitle);
		imageView = (ImageView) findViewById(R.id.iv_brokerage_details_db_pic);
	}

	/**
	 * 佣金详情请求
	 */
	private void RequestBrokerageDetail(String appKey, String tradeId, String detailId, String investDetailId)
	{
		// 将用户输入的信息拼接成字符串发送到服务器
		String inputStr = "service=appBrokerageDetail&tradeId=" + tradeId + "&detailId=" + detailId + "&investDetailId="
				+ investDetailId;
		// 用私钥进行AES加密
		encryptBase(inputStr, C.PRIVATE_KEY);
		// 将用公钥加密后的私钥、用私钥加密后的数据进行封装
		BaseAction action = new BaseAction(encodedData, encriptData, appKey);
		RequestBrokerageDetailResponse requestBrokerageDetailResponse = new RequestBrokerageDetailResponse();
		// 请求服务器
		Request request = new Request(action, requestBrokerageDetailResponse, ActionType.REQUEST_BROKERAGE_DETAIL);
		netManager.excute(request, this, this);
		showProgress("正在获取佣金详情，请稍候");
	}

	private double div(double v1, double v2, int scale)
	{
		if (scale < 0)
		{
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}
		BigDecimal b = new BigDecimal(Double.toString(v1));
		BigDecimal one = new BigDecimal(Double.toString(v2));
		return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

	/**
	 * 请求成功返回的函数
	 */
	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		switch (r.getType())
		{
			case ActionType.REQUEST_BROKERAGE_DETAIL:
				RequestBrokerageDetailResponse requestBrokerageDetailResponse = (RequestBrokerageDetailResponse) r.getResponse();
				investorName.setText("投资人姓名：" + investorNameString);
				investMoney.setText("投资金额：￥" + div(requestBrokerageDetailResponse.investMoney, 100, 2));
				long date = requestBrokerageDetailResponse.investData;
				DateFormat dFormat = new DateFormat();
				String dateFormated = (String) dFormat.format("yyyy-MM-dd", date);
				investData.setText("投资日期：" + dateFormated);
				inCome.setText("我的佣金：￥" + div(requestBrokerageDetailResponse.inCome, 100, 2));
				projectName.setText("项目名称：" + requestBrokerageDetailResponse.projectName);
				needMoney.setText("需求金额：￥" + requestBrokerageDetailResponse.needMoney / 100);
				yield.setText("年华收益率：" + requestBrokerageDetailResponse.yield);
				deadline.setText("期限：" + requestBrokerageDetailResponse.deadline
						+ ConvertUtils.checkTimeUnit(requestBrokerageDetailResponse.deadlineUnit));
				payType.setText("兑换方式：" + "到期兑付本金及收益");
				long startDate = requestBrokerageDetailResponse.startTime;
				long endDate = requestBrokerageDetailResponse.endTime;
				if ((requestBrokerageDetailResponse.startTime + "").equals("0"))
				{
					startTime.setText("成立日期：" + "待成立");
				}
				else
				{
					Date sDate = new Date(startDate);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					startTime.setText("成立日期：" + sdf.format(sDate));
				}
				if ((requestBrokerageDetailResponse.endTime + "").equals("0"))
				{
					endTime.setText("到期日期：" + "待成立");
				}
				else
				{
					Date eDate = new Date(endDate);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					endTime.setText("到期日期：" + sdf.format(eDate));
				}
				dbTeamTextView.setText("担保机构：" + requestBrokerageDetailResponse.guaranteeName);
				dbWordTextView.setText("担保机构话语：" + requestBrokerageDetailResponse.guaranteeStatement);
				String abcString = requestBrokerageDetailResponse.guaranteeAudit;
				if (abcString.equals("IS"))
				{
					dbTitle.setText("担保函：");
				}
				else if (abcString.equals("NO"))
				{
					dbTitle.setText("担保承诺函：");
				}
				ImageUtils.setImage(requestBrokerageDetailResponse.guaranteeLicenseUrl, imageView);
				break;
			default:
				break;
		}
	}

	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		app.showMsg(errorMsg);
	}

	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}
}
